Dreamweaver Article

How to Develop an Online Shopping Cart (Part One)

Lawrence Cramer

www.cartweaver.com

This is part one of a three-part series on shopping carts. In parts one and two, I will give you the background theory and knowledge to understand how shopping carts work and what you need to know to build one. In part three, I will lead you through actually building a simple shopping cart in ColdFusion using Dreamweaver MX 2004. You can see a hosted version of the shopping cart here.

Today, many more developers are being asked to create e-commerce or shopping cart web sites for their clients or employers. At first, this can seem like a daunting and even frightening task. Let’s ease those fears by demystifying online shopping carts. To simplify things, I will break an e-commerce application, which is a shopping cart, into its basic components and analyze them one by one. As you go, you will progressively see how they work together.

There are many ways to approach the actual development, but regardless of size, server platform, database type or structure, the underlying logic remains the same. Once you have a handle on the basics, the details become clearer and less overwhelming. For this article, I will cover the first two components you will need to understand to get started.

The Cart

The logic behind an online shopping cart is actually quit simple. You simply need to remember what the customer wants to buy until they "check out" and place their order.

The Database

First, you must show the customer what you have to sell. You don't have to have a database for this, but you should. Then, once the customer places their order, you must capture the order for your records. This is where you use a database.

Cart Logic

The scarecrow in The Wizard of Oz comes to mind. He says, "If I only had a brain!" Likewise, browsers don't remember. Neither do web servers!

The Stateless Web

To sell on the web, you need to remember what shoppers choose to buy. By nature, web servers and browsers can't do this. Browsers ask and receive. Web servers take requests and deliver files. That's it. They are not designed to remember anything. They are just plain dumb that way.

Working Around Statelessness

The web is "stateless," meaning that it doesn't maintain a connection between you and the server. A cart system must overcome this fact. It's essential to get your head around this concept to understand how cart applications work, regardless of size or complexity. Everything you do in the planning and development of the cart portion of your application revolves around the web's statelessness and how to overcome it.

Filling the Requirements

Server developers have built a number of features into their application servers to work around this problem. Browser developers have chimed in with a number of features to help out. Things are much better than they used to be, but there are still holes. Your job as a developer/programmer is to do all you can to ensure your users don't fall through any of these holes. There are three basic strategies you can use: cookies, session variables, and hybrid solutions.

Cookies—Making the Browser "Remember"

Cookies give the server something to look for. They are small text files stored on the user's system. You can store the cart contents in one of these files. The application server checks for the "cart cookie" and if it finds one, the cookie "reminds" it on what the customer has previously placed in the cart.

Strengths:

  • Requires no server memory
  • Can persist over time, from visit to visit

Weaknesses:

  • The user can turn off cookies.
  • Can only hold a very limited amount of information
  • Can contain only simple data structures; lists only, no structures or arrays

Sessions—Making the Server "Remember"

Session management remembers your current machine ID or Session ID from request to request within a given period of time or until the session ends. Sessions are retained in the application server's memory and serve the same function as a cookie. That is, they remember what the customer has previously chosen by storing that information and recognizing the user's ID so it can reconnect the two from request to request.

Strengths:

  • Are memory resident, therefore they are fast
  • Do not require cookies
  • Can contain complex data such as arrays and structures

Weaknesses:

  • Do not persist. They time out after set time period.
  • Often cannot cross servers in a clustered environment
  • Often cannot support shared SSLs
  • Can increase server load and can slow performance on a busy server

Hybrid Solutions—Trying to Fill the Gaps

With hybrid solutions, server developers try to overcome the shortcomings of cookies and sessions by themselves. There are a number of hybrid solutions to consider. For example, Macromedia ColdFusion offers a "client scope" that it sets a cookie if the user's system allows cookies or will retain the information in memory, like a session, if the user's system doesn't allow cookies. ASP.NET sessions offer a similar solution. There are also do-it-yourself hybrid solution that use a database.

As long as you depend on some kind of relationship between the server and the browser, there is a potential for failure. Fortunately, most of the wholes have been plugged. But if you want to be absolutely sure, there are additional steps you can take. One of these is to write the cart to a temporary "holding table" in your database with a UUID (Unique User ID) and set a session, cookie, or client variable with the same UUID to synchronize the user and cart.

Strengths:

  • Persists as long as you specify
  • If the session or cookie is deleted, you can have the user "sign in" to sync up with the cart.
  • Gives you a record of what users have placed in their cart—even if they don't buy.

Weaknesses:
  • Increases database load
  • Increases database maintenance
  • Additional code complexity
  • Depending on your database system, it can perform slower.

Make a Choice—Pick the Solution That's Best for You

This is where you need to learn what the options are in your sever language of choice and pick the one that is best for you. A little homework and investigation can save you a considerable amount of trouble later. Take a look at what's out there and do some reading. Go through a few tutorials to see how things work so you get a sense of what is right for your particular situation.

Look Ahead!

As important as it is for you to not waste time and resources over engineering your application, it's just as important—in fact more important—that you don't under engineer it. Go ahead and start out simple, but be sure what you set up is extendable. Be sure you don't paint yourself into a corner. Nowhere is this more important than in the database! Which brings us to our next subject.

Data Basics—It All Starts Here

The single most important component of an e-commerce site is the database. Yet, it is often the component that receives the least attention. It's a decision you are going to have to live with…for a long time!

You can develop the code on the front end, even the entire cart, with relative ease. You can even switch to an entirely different server model if you wish. But once you have thousands of records in your database and virtually your entire business is tied into it, changing your database becomes a job of epic proportions.

You may say, "But I'm a web developer, not a database programmer."

That may be true, but if you are developing e-commerce applications, you are going to have to know how a database works and how to work with them. This topic is enough to fill volumes, so I am only going to cover the basics. First, let's look at database types.

Database Types: Will That Be Filed Or Served?

There are basically two types of databases: file-based and server-based.

File-Based Databases

File-based databases are self-contained executables that run as stand-alone programs. A single compiled program contains the data, the UI to maintain the data, and the database engine to interpret queries and serve the data. Common file databases are Microsoft Access, FileMaker Pro, and Quarto Pro.

Strengths:

  • Cheap
  • Easy to run and learn
  • Portable

Weaknesses:

  • Not secure
  • Very limited capacity
  • Limited number of concurrent users
  • Only moderate data integrity
  • Can become very slow

Server-Based Databases

As the name indicates, these databases run on a server engine. The database or databases are not a part of the executable/server program itself. The data is served much the same way file sharing or web servers serve up files. Note: This is a very simplistic explanation. Common database servers include Microsoft SQL Server, MySQL, Oracle, Postgres, and Dbase.

Strengths:

  • Fast
  • High data capacity
  • Support for a large number of concurrent users
  • Greater data integrity
  • Very secure

Weaknesses:

  • More expensive
  • More complex
  • Less portable and must run on the database server

So which is the best choice? Well, before I get into that, let's look at something else: database structure. There are two general types of database structure. Again, remember, I speak in very general terms here.

Database Structure—Flat File Databases

The most basic of flat databases is a simple comma-delimited text file. And believe it or not, in some circumstances, this is all a site may need. A flat file database is similar to a spreadsheet that you would create in Microsoft Excel. A flat file database simply stores data in tables, columns, and rows. This may sound simple and ineffective, but one of the most popular databases on the web, MySQL, is a primarily flat file database.

Note: Version 4 has advanced in the area, but it still cannot be considered a true relational database.

Strengths:

  • Cheap
  • Small, very compressible
  • Fast
  • Most are fairly easy to administer and maintain

Weaknesses:

  • Force all SQL functions to be in the application
  • Do not support advanced database functions, such as views or stored procedures
  • Not as secure, or no security at all

Database Structure—Relational Databases

A relational database stores data in the exact same manner as a flat file database: in tables, columns, and rows. The big difference, however, is that you can establish relationships between the data in one table with the data in another table and enforce referential integrity to prevent lost or "orphan" data. These databases also allow you to internalize many of the functions you would otherwise have to program into your application, such as commonly used queries. This is far too deep a topic to cover here, but suffice it to say that relational databases are far more complex and powerful than their flat-file counterparts.

Strengths:

  • Fast very robust
  • High data capacity
  • Supports a large number of concurrent users
  • Greater data integrity
  • Very secure

Weaknesses

  • More expensive
  • More complex
  • Less portable, and must run on the database server
  • More difficult to maintain and administer

So which is the best choice? Which way should you lean?

Choosing What's Best For You: File Database or Database Server?

Let's help make this part a little easier. With the exception of Microsoft Access, all file databases are pretty much useless when it comes to delivering dynamic web content—in spite of what the ads might say. Save yourself a great deal of time and trouble and accept this fact right now.

Speaking of Microsoft Access, believe it or not, the most common database on the web is Microsoft Access. The database engine in Microsoft Access 2000 was significantly improved and for a small, low-traffic sites; Microsoft Access does an admirable job. Additionally, Access is a true relational database and supports views. But it has its limitations. It simply cannot handle heavy traffic. If your site traffic is likely to generate more than 20 concurrent database hits at a time, it's time to move to a database server like Oracle, SQL Server, or MySQL (if you don’t need a relational database). Generally, database servers are the way to go for the web.

So Which is It, Flat or Relational?

There are two things to consider when deciding on a database.

  1. Size: The number of records you will store in a database is a big concern. If you have just a few records or even a few hundred, and you know the size is will stay that way, a flat-file database will most likely work for you. If you are going to have thousands of records and the database will continue to grow, then look at a more powerful relational database.
  2. Traffic: In a site with heavy traffic, a relational database, especially a server-based one, such as SQL Server or Oracle or the like, can really speed up performance by internalizing many of the common queries and actions. You can perform a great deal of the maintenance and data compiling in advance and store it in memory.

Bring It Together

Now, with your database and your cart, the web has a brain. Your application can display products, and remember and record who your customers are and what they order. Take a look at the demo that you will build in part three of this article series to see a basic cart in action. You can find it here. The demo cart is written in ColdFusion and uses a Microsoft Access database, but the principles and logic involved are pretty much the same in any server language.

In the next article, I will discuss the basics of business logic behind an online shopping cart, discuss accepting payments online, and security.


About the author

Lawrence's day job is web application development. He also develops Dreamweaver Extensions, helps out on the Macromedia forums, teaches at the local Community College, speaks at web development conferences around the country, and writes articles on web development and e-commerce. For fun, he takes motorcycle road trips and occasionally catches a fish or two with his wife of 25+ years. Lawrence has three children—two daughters and one son. Two are grown and one is still in school. You can find him at lawrence@cartweaver.com.